<template>
  <div>this is department</div>
</template>
<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <el-col>操作</el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" :props="defaultProps" :default-expand-all="true">
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="showDialog(data.id)"
                          >添加子部门</el-dropdown-item
                        >
                        <el-dropdown-item @click.native="editDepart(data.id)"
                          >编辑部门</el-dropdown-item
                        >
                        <el-dropdown-item @click.native="newDelDepart(data.id)"
                          >删除部门</el-dropdown-item
                        >
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      title="提示"
      :visible="dialogVisible"
      width="30%"
      center
      @close="hideDialog"
      @open="openDialog"
    >
      <template #default>
        <el-form ref="addForm" label-width="120px" :model="form" :rules="rules">
          <el-form-item label="部门名称" prop="name">
            <el-input v-model="form.name" style="width: 80%" placeholder="1-50个字符" />
          </el-form-item>
          <el-form-item label="部门编码" prop="code">
            <el-input v-model="form.code" style="width: 80%" placeholder="1-50个字符" />
          </el-form-item>
          <el-form-item label="部门负责人" prop="manager">
            <el-select v-model="form.manager" style="width: 80%" placeholder="请选择">
              <el-option
                v-for="item in managerList"
                :key="item.id"
                :label="item.username"
                :value="item.username"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="部门介绍" prop="introduce">
            <el-input
              v-model="form.introduce"
              style="width: 80%"
              placeholder="1-300个字符"
              type="textarea"
            />
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" size="small" @click="confirm">确定</el-button>
          <el-button size="small" @click="dialogVisible = false">取消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import transTree from '@/utils/transTree'
import {
  fetchDepartmentList,
  getManagerList,
  addDepart,
  delDepart,
  updateDepart,
  fetchDetail
} from '@/api/department'
export default {
  data() {
    const validCode = (rule, value, callback) => {
      let codeList = []
      if (this.form.id) {
        codeList = this.newCode.filter(item => item !== this.cacheCode)
      } else {
        codeList = this.newCode
      }
      console.log(codeList)
      if (this.newCode.includes(value)) {
        callback(new Error('code重复请更换code'))
      } else {
        callback
      }
    }
    return {
      // 依赖一份树形数据
      list: [
        {
          name: '财务部',
          manager: 'hh',
          children: [
            {
              name: '财务核算部',
              manager: 'xx'
            },
            {
              name: '税务核算部',
              manager: 'zz'
            }
          ]
        }
      ],
      defaultProps: {
        label: 'name'
      },
      dialogVisible: false,
      form: {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: '' // 当前要新增de子部门
      },
      rules: {
        code: [
          { required: true, message: '部门编码为必填项', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码长度为1-50字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍为必填项', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍长度为1-300字符', trigger: 'blur' }
        ],
        manager: [{ required: true, message: '部门负责人为必填项', trigger: 'change' }],
        name: [
          { required: true, message: '部门编码为必填项', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码长度为1-50字符', trigger: 'blur' }
        ]
      },
      managerList: [],
      newCode: [],
      cacheCode: null
    }
  },
  mounted() {
    this.getlist()
  },
  methods: {
    async getlist() {
      const res = await fetchDepartmentList()
      this.list = transTree(res.depts)
      // console.log(this.list)
      this.newCode = res.depts.map(item => item.code)
    },
    showDialog(id) {
      this.form.pid = id
      this.dialogVisible = true
    },
    hideDialog() {
      this.dialogVisible = false
      this.$refs.addForm.resetFields()
    },
    async openDialog() {
      // console.log('dialog open')
      const res = await getManagerList()
      this.managerList = res
    },
    confirm() {
      // 兜底校验
      this.$refs.addForm.validate(async valid => {
        if (valid) {
          if (this.form.id) {
            await updateDepart(this.form)
          } else {
            await addDepart(this.form)
          }

          this.$message.success(`${this.form.id ? '编辑' : '新增'}部门成功`)
          this.dialogVisible = false
          this.getlist()
          this.$refs.addForm.resetFields()
          this.resetForm()
        }
      })
    },
    resetForm() {
      this.form = {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: ''
      }
    },
    async newDelDepart(id) {
      await delDepart(id)
      this.getlist()
    },
    async editDepart(id) {
      this.dialogVisible = true
      const res = await fetchDetail(id)
      console.log(res)
      this.form = res
      this.cacheCode = res.code
    }
  }
}
</script>
